import datetime
from classes.track import FlacTrack
from mutagen.flac import FLAC


def listmerge(l1: list, l2: list) -> list:
    new_l1 = l1
    for el in l2:
        new_l1.extend(el)


class NotTheSameTracks(Exception):
    pass


class Album:
    tracks: list[FlacTrack]

    def __init__(self, trackspaths) -> None:
        self.paths = trackspaths
        self.importTracks()
        self.compareTracks()

    def importTracks(self) -> None:
        self.tracks = []
        for path in self.paths:
            if path.endswith(".flac"):
                track = FlacTrack(path)
                self.tracks.append(track)
        self.tracks.sort(key=lambda x: int(x.tracknumber))

    def compareTracks(self) -> None:
        album = self.tracks[0].album
        albumartist = self.tracks[0].albumartist
        label = self.tracks[0].copyright
        releasedate = self.tracks[0].getReleaseDate()

        for track in self.tracks:
            if (
                track.album != album
                or track.albumartist != albumartist
                or track.copyright != label
                or track.getReleaseDate() != releasedate
            ):
                raise NotTheSameTracks(Exception)

    def getLabel(self) -> str:
        return self.tracks[0].label

    def getReleaseDate(self) -> datetime.datetime:
        return self.tracks[0].getReleaseDate()

    def getGenre(self, splitter: str) -> list[str]:
        genre = []
        for track in self.tracks:
            genre.extend(track.getGenre(splitter))
        return list(dict.fromkeys(genre))

    def getSubgenre(self, splitter: str) -> list[str]:
        subgenre = []
        for track in self.tracks:
            subgenre.extend(track.getSubgenre(splitter))
        return list(dict.fromkeys(subgenre))

    def getArtist(self) -> list[str]:
        artist = []
        for track in self.tracks:
            artist.extend(track.getArtist())
        print(artist)
        return list(dict.fromkeys(artist))

    def getAlbumArtist(self) -> str:
        return self.tracks[0].getAlbumArtist()

    def getAlbum(self) -> str:
        return self.tracks[0].album

    def __str__(self) -> str:
        return (
            f"Artist: {self.getArtist()}\n"
            f"Date: {self.getReleaseDate()}\n"
            f"Album: {self.getAlbum()}\n"
            f"Album Artist: {self.getAlbumArtist()}\n"
            f"Album Genre: {self.getGenre()}\n"
            f"Album Subgenre: {self.getSubgenre()}\n"
            f"Copyright: {self.getLabel()}\n"
        )
